package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"zhihu/models/mysql"

	"zhihu/zhihurpc/internal/svc"
	"zhihu/zhihurpc/zhihurpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserPerfectLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserPerfectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserPerfectLogic {
	return &UserPerfectLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserPerfectLogic) UserPerfect(in *zhihurpc.UserPerfectRequest) (*zhihurpc.UserPerfectResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	mysql.DB.Debug().Where("id = ?", in.UserId).Find(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("该用户不存在，禁止完善信息")
	}
	user = mysql.User{
		Model:     gorm.Model{ID: uint(in.UserId)},
		HeadImage: in.HeadImage,
		NickName:  in.NickName,
		Sex:       in.Sex,
		Desc:      in.Desc,
		Address:   in.Address,
	}
	err := mysql.DB.Debug().Updates(&user).Error
	if err != nil {
		return nil, fmt.Errorf("用户信息完善失败")
	}
	return &zhihurpc.UserPerfectResponse{
		UserId: int64(user.ID),
	}, nil
}
